package com.day1013.morning;
//  抢票线程类   ????????????????????????????????????//
public class TicketThread1 implements Runnable {
    private int count = 10;//  剩余票数
    private int num = 0; //  抢到的票数
    private boolean flag = true;

    public synchronized boolean sale() {
        if (count <= 0) {
            flag = false;
            return flag;
        }
        num++;
        count--;
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String name = Thread.currentThread().getName();
        System.out.println(name + "抢到了第" + num + "张票，还剩" + count + "张票。");
        if ("黄牛".equals(Thread.currentThread().getName()) && num == 1) {
            return false;
        }
        return true;
    }

    @Override
    public void run() {
//        while (!sale()){
//            if (flag == false){
//                break;
//            }
//        }
        while (true){
            if (!sale()){
                break;
            }

        }
    }
}
